package com.group11.resume.controller;

import com.group11.common.pojo.ResponseResult;
import com.group11.resume.VO.ResumeVO;
import com.group11.resume.entity.Resume;
import com.group11.resume.service.ResumeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping
public class ResumeController {
    @Autowired
    private ResumeService resumeService;

    //创建简历
    @PostMapping("createResume")
    public ResponseResult<Void> createResume(@RequestHeader("id") Integer id,
                                             @RequestBody ResumeVO resumeVO) {
        resumeVO.getResume().setUserId(id);
        resumeService.createResume(id,resumeVO);
        return ResponseResult.SUCCESS;
    }

    @PostMapping("updateResume")
    public ResponseResult<Void> updateResume(@RequestHeader("id") Integer id,
                                             @RequestBody Resume resume) {
        resumeService.updateResume(resume);
        return ResponseResult.SUCCESS;
    }


    //根据用户id查找简历列表
    @PostMapping("listByUserId")
    public ResponseResult<List<Resume>> listByUserId(@RequestHeader("id") Integer id){
        List<Resume> resumes = resumeService.listByUserId(id);
        return new ResponseResult<>(resumes);
    }

    //查看简历详情
    @PostMapping("viewResume")
    public ResponseResult<ResumeVO> viewResume(@RequestHeader("id") Integer id,
                                             @RequestBody Integer resumeId){
        ResumeVO resumeVO = resumeService.getById(resumeId);
        return new ResponseResult<>(resumeVO);
    }

    //批量删除简历
    @PostMapping("deleteResume")
    public ResponseResult<Void> deleteResumes(@RequestHeader("id") Integer id,
                                             @RequestBody List<Integer> resumeIds){
        resumeService.deleteResumes(id,resumeIds);
        return ResponseResult.SUCCESS;
    }

    //刷新简历
    @PostMapping("refreshResume")
    public ResponseResult<Void> refreshResume(@RequestHeader("id") Integer id,
                                             @RequestBody Integer resumeId){
        resumeService.refreshResume(resumeId);
        return ResponseResult.SUCCESS;
    }

    //简历设为默认
    @PostMapping("setDefaultResume")
    public ResponseResult<Void> setDefaultResume(@RequestHeader("id") Integer id,
                                                 @RequestBody Integer resumeId){
        resumeService.setDefaultResume(id,resumeId);
        return ResponseResult.SUCCESS;
    }

    @PostMapping("getById")
    public ResponseResult<?> getById(@RequestParam int id) {
        Resume resume = resumeService.getById(id).getResume();
        if (resume != null) {
            return new ResponseResult<>(resume);
        }
        return ResponseResult.FAILED;
    }
}
